package phapp.service.qfd.impl;

import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import phapp.service.common.*;
import cn.org.rapid_framework.page.Page;
import cn.org.rapid_framework.page.PageRequest;
import javacommon.base.*;
import phapp.model.custom.Custom;
import phapp.model.custom.CustomFavorite;
import phapp.model.qfd.*;
import phapp.dao.qfd.*;
import phapp.service.qfd.impl.*;
import phapp.service.qfd.*;

/**
 * Title: t_qfd_data 业务处理层 Description: spring事务实现层,调用Dao层取数据 Copyright:
 * Copyright (c) 2012-10 Company:
 * 
 * @author you name
 * @version 1.0
 */

@Component("qfdManager")
@Transactional
public class QfdManagerImpl extends BaseManager<Qfd, java.lang.Integer> implements QfdManager
{
   public static final String QFD_IMG_URL_PREFIX = "http://117.40.178.58:9090/qfd/qfdimg";
   public static final String QFD_IMG_FILE_PATH = "/app/qfdapp/webapps/qfd/qfdimg";
   
//   public static final String QFD_IMG_URL_PREFIX = "http://localhost:8080/qfd/qfdimg";
//   public static final String QFD_IMG_FILE_PATH = "F:/Code/EclipseENV32/.metadata/.plugins/org.eclipse.wst.server.core/tmp0/wtpwebapps/qfd/qfdimg";

   
   private QfdDao qfdDao;

   public void setQfdDao(QfdDao dao)
   {
      this.qfdDao = dao;
   }

   /** 获取Dao */
   public EntityDao getEntityDao()
   {
      return this.qfdDao;
   }

   /**
    * <p>
    * 分页查询Qfd<br>
    * 
    * @param PageRequest
    *           pr
    * @return Page
    * @author
    */
   @Transactional(readOnly = true)
   public Page findByPageRequest(PageRequest pr)
   {
      return qfdDao.findByPageRequest(pr);
   }

   /**
    * <p>
    * 批量删除 Qfd<br>
    * 
    * @param Qfd
    *           u
    * @return Boolean
    * @author
    */
   public Boolean removeByIdArry(Qfd u)
   {
      Boolean result = false;
      int i = -1, size = u.getIdList().size();
      i = qfdDao.removeByIdArry(u);
      if(i == size)
      {
         result = true;
      }
      else
      {
         throw new RuntimeException();
      }
      return result;
   }

   public int saveData(Qfd qfdData, int nModiFlag)
   {
      int nStatus = 0, i = 0;
      String strFullFileName = "", strFileUrl = "";
      //Map<String, Object> mapQuery = new HashMap<String, Object>();

      try
      {
         File[] fileUploadSet = qfdData.getUploadFileSet();
         if(fileUploadSet != null)
         {
            String[] atttachFileSet = new String[fileUploadSet.length];
            for(i = 0; i < fileUploadSet.length; i++)
            {
               String FileSeq = CommonUtils.generateSeq();
               atttachFileSet[i] = QFD_IMG_FILE_PATH + File.separator + FileSeq + "_" + qfdData.getUploadFileNameSet()[i];
               
               strFullFileName = atttachFileSet[i];
               strFullFileName = strFullFileName.replaceAll(" ", "");
               CommonUtils.saveUploadFile(fileUploadSet[i], strFullFileName);
            }
      
            File tmpFile = new File(strFullFileName);
            strFileUrl = QFD_IMG_URL_PREFIX + "/" + tmpFile.getName();
            strFileUrl = strFileUrl.replaceAll(" ", "");
            
            qfdData.setImgUrl(strFileUrl);
            qfdData.setImgFile(strFullFileName);
            System.out.println("strFileUrl[" + strFileUrl + "] - strFullFileName[" + strFullFileName + "]\n\n\n");
         }
         
         if(nModiFlag == 0)
         {
            qfdData.setPopularCount(0);
            qfdData.setSaleCount(0);
            qfdData.setQfdStatus(1);
            save(qfdData);
         }
         else
         {
            update(qfdData);
         }
         
         nStatus = 0;
      }
      catch(Exception eLogin)
      {
         nStatus = -1;
         eLogin.printStackTrace();
      }
      
      return nStatus;
   }
   
   
   public Map getLightData(JSONObject jsonData)
   {
      String strType = "";
      Qfd qfdData = null;
      JSONArray arrayData = null;
      JSONObject jsonQfd = null;
      List<Qfd> listQuery = null;
      int nStatus = 0, i = 0;
      int nChargeFlag = 0, nPopularFlag = 0, nSellFlag = 0;
      Map<String, Object> mapResult = new HashMap<String, Object>();
      Map<String, Object> mapQuery = new HashMap<String, Object>();

      try
      {
         strType = jsonData.getString("qfdtype");
         nSellFlag = jsonData.getInt("sellflag");
         nChargeFlag = jsonData.getInt("chargeflag");
         nPopularFlag = jsonData.getInt("popularflag");
         
         mapQuery.clear();
         if(nChargeFlag == 0 || nChargeFlag == 1)
            mapQuery.put("chargeFlag", jsonData.getInt("chargeflag"));
         if(strType.compareToIgnoreCase("全部") != 0)
            mapQuery.put("qfdType", jsonData.getString("qfdtype"));
//         mapQuery.put("popularflag", jsonData.getInt("popularflag"));
//         mapQuery.put("sellflag", jsonData.getInt("sellflag"));
         listQuery = qfdDao.findList(mapQuery);
         
         arrayData = new JSONArray();
         for(i=0; i<listQuery.size(); i++)
         {
            qfdData = listQuery.get(i);
            jsonQfd = new JSONObject();
            jsonQfd.put("qfdId", qfdData.getQfdId());
            jsonQfd.put("qfdName", qfdData.getQfdName());
            jsonQfd.put("imgUrl", qfdData.getImgUrl());
            jsonQfd.put("chargeFlag", qfdData.getChargeFlag());
            jsonQfd.put("qfdType", qfdData.getQfdType());
            jsonQfd.put("qfdPrice", qfdData.getQfdPrice());
            jsonQfd.put("saleCount", qfdData.getSaleCount());
            jsonQfd.put("popularCount", qfdData.getPopularCount());
            
            arrayData.add(jsonQfd);
            jsonQfd = null;
         }
         
         mapResult.put("qfdlist", arrayData);
         nStatus = 1000;
      }
      catch(Exception eLogin)
      {
         nStatus = 10000;
         eLogin.printStackTrace();
      }
      finally
      {
         mapResult.put("statusCode", nStatus);
         if(nStatus == 1000)
            mapResult.put("message", "操作成功！");
         else if(nStatus == 2000)
            mapResult.put("message", "用户不存在！");
         else if(nStatus == 2004)
            mapResult.put("message", "密码错误！");
         else
            mapResult.put("message", "操作失败！");
      }
      
      return mapResult;
   }
   
}
